package ControlLayer;

import java.util.ArrayList;
import java.util.Date;

import DBLayer.DbConnection;
import DBLayer.DbSaleOrder;
import DBLayer.IfDbSaleOrder;
import ModelLayer.Customer;
import ModelLayer.Invoice;
import ModelLayer.SalesOrder;

public class OrderCtrl
{
	public ArrayList<SalesOrder> findAllSaleOrders()
    {
        IfDbSaleOrder dbSaleOrders = new DbSaleOrder();
        ArrayList<SalesOrder> allSaleOrders = new ArrayList<SalesOrder>();
        allSaleOrders = dbSaleOrders.getAllSaleOrders(false);
        return allSaleOrders;
    }
    public SalesOrder findSaleOrderByOrderNr(int orderNr)
    {
    	IfDbSaleOrder dbSaleOrders = new DbSaleOrder();
        return dbSaleOrders.findSalesOrderByOrderNr(orderNr, true);
    }
    public void insertSaleOrder(int orderNr, Date date, String deliveryStatus, Date deliveryDate, double totalPrice, Date returnDate, String orderType, Customer customer, Invoice invoice)
    {
    	SalesOrder saleorderObj = new SalesOrder();
    	saleorderObj.setOrderNr(orderNr);             
    	saleorderObj.setDate(date);
    	saleorderObj.setDeliveryStatus(deliveryStatus);
    	saleorderObj.setDeliveryDate(deliveryDate);
    	saleorderObj.setTotalPrice(totalPrice);
    	saleorderObj.setReturnDate(returnDate);
    	saleorderObj.setOrderType(orderType);
    	saleorderObj.setCustomer(customer);
    	saleorderObj.setInvoice(invoice);
 
        try
        {
            DbConnection.startTransaction();
            DbSaleOrder dbSalesOrder = new DbSaleOrder();
            dbSalesOrder.insertSalesOrder(saleorderObj);
            DbConnection.commitTransaction();
        }
        catch(Exception e)
        {
            DbConnection.rollbackTransaction();
        }
    }

    public int updateSaleOrder(int orderNr, Date date, String deliveryStatus, Date deliveryDate, double totalPrice, Date returnDate, String orderType, Customer customer, Invoice invoice)
    {
    	IfDbSaleOrder dbSalesOrder = new DbSaleOrder();
    	SalesOrder saleorder = new SalesOrder();
    	saleorder.setOrderNr(orderNr);             
    	saleorder.setDate(date);
    	saleorder.setDeliveryStatus(deliveryStatus);
    	saleorder.setDeliveryDate(deliveryDate);
    	saleorder.setTotalPrice(totalPrice);
    	saleorder.setReturnDate(returnDate);
    	saleorder.setOrderType(orderType);
    	saleorder.setCustomer(customer);
    	saleorder.setInvoice(invoice);
        return dbSalesOrder.updateSalesOrder(saleorder);
    }
    
    public int deleteSaleOrder(int orderNr)
    {
    	IfDbSaleOrder dbCust = new DbSaleOrder();
    	return dbCust.deleteSalesOrder(orderNr);
    }
}
